package cn.gson.crm.control.group;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.AllotDao;
import cn.gson.crm.model.dao.GroupDao;
import cn.gson.crm.model.entity.Group;
import cn.gson.crm.model.entity.GroupVo;
import cn.gson.crm.model.po.StaffPo;

/**
 * Servlet implementation class GroupServlet
 */
@WebServlet("/group")
public class GroupServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
     private GroupDao gro=new GroupDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GroupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method=request.getParameter("method");
		if("insert".equalsIgnoreCase(method)){
			insert(request,response);
		}
		if("select".equalsIgnoreCase(method)){
			select(request,response);
		}
		if("selectname".equalsIgnoreCase(method)){
			selectname(request,response);
		}
		if("update".equalsIgnoreCase(method)){
			update(request,response);
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String clazzId=request.getParameter("clazz");
		String stuId=request.getParameter("studentId");
		boolean boo=gro.updateGroup(stuId, clazzId);
		if(boo){
			redirect("student?method=selectby");
		}		
	
	}

	private void selectname(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("name");
		String size=request.getParameter("count");
		List<GroupVo> list=gro.getStudentNameByClazzName(name);
		request.setAttribute("name", name);
		request.setAttribute("list", list);
		request.setAttribute("size", size);
		forward("group/groupname.jsp");
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<GroupVo> gvo=gro.getGroupBy();
		request.setAttribute("grolist", gvo);
		List<StaffPo> data =new AllotDao().getStaffList();
		if(data != null){
			request.setAttribute("StaffList", data);
		}
		forward("group/group2.jsp");
	}

	private void insert(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String clazzId=request.getParameter("clazz");
		String stuId=request.getParameter("studentId");
		Group group=new Group();
		group.setGroupClazzId(Integer.parseInt(clazzId));
		group.setGroupStudentId(Integer.parseInt(stuId));
		boolean boo=gro.insertGroup(group);
		if(boo){
			redirect("student?method=selectby");
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		get(request, response);
	}

}
